#!usr/bin/env python3
from flask_mail import Message
from flask import render_template
from threading import Thread
from config import getLang #获取语言包的函数 getLang(key,args)
from . import mail,create_app

app = create_app("df")

#异步发送邮件
def send_async_mail(app,msg):
    with app.app_context():
        mail.send(msg)

'''
发送邮件
@param toList 接收列表
@param title  邮件标题
@param tplName 邮件目标名(txt和html文件)
@param isAsync 是否异步发送
@param kw      目标的参数
'''
def send_mail(toList,title,tplName,isAsync=False,**kw):
    msg = Message(title,sender='623097864@qq.com',recipients=toList)
    msg.body = render_template('mail/'+tplName+'.txt',**kw)
    msg.html = render_template('mail/'+tplName+'.html',**kw)
    if isAsync:
        thr = Thread(target=send_async_mail,args=[app,msg])
        thr.start()
        return thr
    else:
        mail.send(msg)
        return None

#发送验证邮件的函数
def send_confirm(u):
    send_mail([u.email],getLang('regEmailTitle'),'confirm',user=u,token=u.make_id_token()) #发送验证邮件